package com.chris.blog.controller;

import com.chris.blog.entity.po.UserInfoPO;
import com.chris.blog.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class UserInfoController {

    @Autowired
    UserInfoService userInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private BlogService blogService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private CommentService commentService;

    @GetMapping("/userinfo/setting/{uid}")
    public String info(@PathVariable String uid, Model model) {
        callbackInfo(uid, model);
        return "user/setting";
    }


    // 根据uid 获取用户信息
    public void callbackInfo(String uid, Model model) {
        // 获取用户信息
        UserInfoPO userInfoPO = userInfoService.selectInfoByUid(uid);
        model.addAttribute("userInfo", userInfoPO);
        // 如果兴趣栏不为空 也要回显
        String hobby = userInfoPO.getHobby();
        // 不为null 也不为""时
        if (hobby != null && !hobby.isEmpty()) {
            String[] hobbies = hobby.split(",");
            model.addAttribute("hobbies", hobbies);
        }
        // 回显 问题数  博客数 回复数
        int qcount = questionService.count(uid);
        int bcount = blogService.count(uid);
        int ccount = commentService.count(uid);
        model.addAttribute("qcount", qcount);
        model.addAttribute("bcount", bcount);
        model.addAttribute("ccount", ccount);
    }

    @PostMapping("/user/update/{uid}")
    public String update(@PathVariable String uid, UserInfoPO userInfoPO) {
        userInfoService.updateByUid(userInfoPO);
        return "redirect:/user/" + uid;
    }
}
